
package com.iris.egrant.service.formInit;

import com.iris.egrant.exception.DaoException;
import com.iris.egrant.exception.ServiceException;
import com.iris.egrant.model.formInit.FormBaseLibrary;
import com.iris.egrant.model.formInit.FormInitItem;
import com.iris.egrant.model.grantSetting.GrantSetting;
import com.iris.egrant.service.proposal.BaseXmlInitServiceImpl;
import com.iris.egrant.app.utils.IrisStringUtils;
import com.iris.egrant.utils.XMLHelper;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Service("InitializeService")
 public class InitializeServiceImpl extends BaseXmlInitServiceImpl implements InitializeService {
    /*  35 */   private static Logger logger = LoggerFactory.getLogger(InitializeServiceImpl.class);


    @Autowired
    private InitalizeReadService initializeService;


    public FormBaseLibrary getFormBaseLibraryByGrantId(Long grantCode, Long formCode, String xmlData, Map<String, Object> codes) throws ServiceException {
        /*  46 */
        FormBaseLibrary formBaseLibrary = null;
        try {

            List<FormInitItem> initbList;
            /*  48 */
            if (formCode == null) {
/*
                */
/*  49 *//*

                GrantSetting gs = null;
                */
/*  50 *//*

                String subGrantNo = (String) codes.get("sub_grant_no");
                */
/*  51 *//*

                Object subGrantCode = codes.get("sub_grant_code");
                */
/*  52 *//*

                if (StringUtils.isNotBlank(subGrantNo)) {
                    */
/*  53 *//*

                    gs = this.initializeService.getGrantInfoByGrantNo(subGrantNo);
                    */
/*  54 *//*

                } else if (subGrantCode != null) {
                    */
/*  55 *//*

                    gs = this.initializeService.getGrantInfoByGrantCode(Long.valueOf(subGrantCode.toString()));

                } else {
                    */
/*  57 *//*

                    gs = this.initializeService.getGrantInfoByGrantCode(grantCode);

                }

                */
/*  60 *//*

                String formType = codes.get("formType").toString();
                */
/*  61 *//*

                Long formId = Long.valueOf(0L);
                */
/*  62 *//*

                if (formType.equalsIgnoreCase("proposal")) {
                    */
/*  63 *//*

                    formId = gs.getPrpFormId();
                    */
/*  64 *//*

                } else if (formType.equalsIgnoreCase("contract")) {
                    */
/*  65 *//*

                    formId = gs.getCtrFormId();
                    */
/*  66 *//*

                } else if (formType.equalsIgnoreCase("acceptance")) {
                    */
/*  67 *//*

                    formId = gs.getAptFormId();
                    */
/*  68 *//*

                } else if (formType.equalsIgnoreCase("project")) {
                    */
/*  69 *//*

                    formId = gs.getPrjFormId();
                    */
/*  70 *//*

                } else if (formType.equalsIgnoreCase("report")) {
                    */
/*  71 *//*

                    formId = gs.getRptFormId();

                }
*/

                /*  74 */
//                formBaseLibrary = this.initializeService.getFormBaseLibraryByFormId(formId);
                formBaseLibrary = this.initializeService.getFormBaseLibraryByFormId(11146L);

            } else {
                /*  76 */
//                formBaseLibrary = this.initializeService.getFormBaseLibraryByFormCode(formCode);
                formBaseLibrary = this.initializeService.getFormBaseLibraryByFormId(11146L);

            }

            /*  79 */
//            if (SecurityUtils.getCurrentUserRoleId() == 26 && grantCode != null && grantCode.longValue() != 5000L) {
//                /*  80 */
//                initbList = this.initializeService.getFormInitItemListById(formBaseLibrary.getDailuInitItems());
//            
//            } else {
            /*  82 */
            initbList = this.initializeService.getFormInitItemListById(formBaseLibrary.getInitItems());

//            }

            /*  85 */
            xmlData = refreshData(initbList, xmlData, (Integer) codes.get("actionType"), codes);
            /*  86 */
            formBaseLibrary.setXmlData(xmlData);
            /*  87 */
        } catch (DaoException e) {
            /*  88 */
            logger.error("没有找到业务类别ID为-->" + grantCode + "的配置", (Throwable) e);
            /*  89 */
            throw new ServiceException("没有找到类型ID为-->" + grantCode + "的配置");

        }

        /*  92 */
        return formBaseLibrary;

    }


    public String refreshData(List<FormInitItem> formInitItems, String xmlData, Integer refrashType, Map<String, Object> codes) throws ServiceException {
        /* 103 */
        if (xmlData == null) {
            /* 104 */
            xmlData = "<data></data>";

        }

        try {
            /* 107 */
            Document doc = DocumentHelper.parseText(xmlData);
            /* 108 */
/*            for (FormInitItem formInitItem : formInitItems) {

                *//* 110 *//*
                if (formInitItem.getInitFlag().longValue() < refrashType.intValue()) {

                    continue;

                }

                *//* 114 *//*
                String type = formInitItem.getType();
                *//* 115 *//*
                String content = formInitItem.getContent();

                *//* 117 *//*
                List<Object> params = new ArrayList();
                *//* 118 *//*
                if (StringUtils.isNotBlank(content)) {
                    *//* 119 *//*
                    content = IrisStringUtils.transSql(content, codes, params);

                }
                *//* 121 *//*
                if (IrisStringUtils.isExistParam(content)) {

                    continue;

                }


                *//* 126 *//*
                if ("SQL".equalsIgnoreCase(type)) {


                    *//* 129 *//*
                    List<Map<String, Object>> list = this.initializeService.getRefreshList(content, params);
                    *//* 130 *//*
                    if (list.size() == 0) {

                        continue;

                    }
                    *//* 133 *//*
                    String node = formInitItem.getNode();

                    *//* 135 *//*
                    if (node == null || "".equalsIgnoreCase(node)) {
                        *//* 136 *//*
                        if (list.size() == 1) {
                            *//* 137 *//*
                            XMLHelper.fillXmlDataFromMap(doc, formInitItem.getXpath(), list.get(0));
                            continue;

                        }
                        *//* 139 *//*
                        for (int j = 0; j < list.size(); j++) {
                            *//* 140 *//*
                            Map<String, Object> map = list.get(j);
                            *//* 141 *//*
                            String value = "";
                            *//* 142 *//*
                            if (map.get("VALUE") != null) {
                                *//* 143 *//*
                                value = map.get("VALUE").toString();

                            }
                            *//* 145 *//*
                            String xpath = map.get("XPATH").toString();
                            *//* 146 *//*
                            String nodeName = map.get("NODE").toString();
                            *//* 147 *//*
                            Map<String, String> tmp = new HashMap<String, String>();
                            *//* 148 *//*
                            tmp.put(nodeName, value);
                            *//* 149 *//*
                            XMLHelper.fillXmlDataFromMap(doc, xpath, tmp);

                        }

                        continue;

                    }
                    *//* 153 *//*
                    String nodeXML = XMLHelper.parseTableToXML(list, node + "s", node);
                    *//* 154 *//*
                    XMLHelper.refreshXmlDataFromNode(doc, formInitItem.getXpath(), nodeXML);

                    continue;

                }
                *//* 157 *//*
                codes.put("xmlData", xmlData);

                *//* 159 *//*
                String[] nodes = formInitItem.getNode().split(",");
                *//* 160 *//*
                String[] xpaths = formInitItem.getXpath().split(",");
                *//* 161 *//*
                Document sDoc = getDocument(type, codes, formInitItem.getContent());
                *//* 162 *//*
                if (sDoc == null) {

                    continue;

                }
                *//* 165 *//*
                for (int i = 0; i < nodes.length; i++) {
                    *//* 166 *//*
                    Node sNode = sDoc.selectSingleNode(nodes[i]);
                    *//* 167 *//*
                    XMLHelper.refreshXmlDataFromNode(doc, xpaths[i], sNode, false, true);

                }

            }*/


            /* 172 */
            xmlData = doc.asXML();

        }
        /* 174 */ catch (DocumentException e) {
            /* 175 */
            logger.error("初始化时XML转换出错", (Throwable) e);
            /* 176 */
            throw new ServiceException("初始化时XML转换出错");

        }
        /* 178 */
        logger.debug(xmlData);
        /* 179 */
        return xmlData;

    }


}


/* Location:              D:\文件kjt\WEB-INF\lib\egrant-code-2.2.20240205.1659.jar!\com\iris\egrant\service\formInit\InitializeServiceImpl.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       1.1.3
 */